package com.vehicle.controller;


import com.vehicle.form.UniversityForm;
import com.vehicle.service.UniversityService;
import com.vehicle.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-05-14
 */
@RestController
@RequestMapping("/university")
public class UniversityController {

    @Autowired
    UniversityService universityService;

//    添加：学校代码，学校名字
   @PostMapping("add/{code}/{name}")
   public ResultVO addUniversity(HttpServletRequest request, @PathVariable("code") String code, @PathVariable("name") String name){
       String ip = request.getRemoteAddr();
       String id = request.getHeader("id");
      return ResultVO.succ(universityService.addUniversity(ip,Integer.parseInt(id),code,name));
   }



//    删除: 学校id
    @DeleteMapping("delete/{univerId}")
    public ResultVO deleteUniversity(HttpServletRequest request,@PathVariable("univerId") Integer univerId){
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
    return ResultVO.succ(universityService.deleteUniversity(ip,Integer.parseInt(id),univerId));
}

//    修改: 根据学校id修改学校名称和学校代码
   @PutMapping("/update")
     public ResultVO updateUniversity(HttpServletRequest request,@RequestBody UniversityForm universityForm){
       String ip = request.getRemoteAddr();
       String id = request.getHeader("id");
       return ResultVO.succ(universityService.updateUniversity(ip,Integer.parseInt(id),universityForm));
   }


//    查询：分页查询所有
    @GetMapping("/allList/{page}/{size}")
     public ResultVO getAllUniversity(@PathVariable("page") String page,@PathVariable("size") String size){
       return ResultVO.succ(universityService.getAllUniversity(Integer.parseInt(page),Integer.parseInt(size)));
     }

     //模糊查询：学校名字
     @GetMapping("/likeList/{univerName}")
     public ResultVO getLikeUniversity(@PathVariable("univerName") String univerName){
         return ResultVO.succ(universityService.getListByNameLike(univerName));
     }


    //查询： Id查询出学校名称
    @GetMapping("/getName/{univerId}")
    public String getNameUniversity(@PathVariable("univerId") Integer univerId){
        return universityService.getUniversityNameById(univerId);
    }

}



